/*
   Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
   Original sources are available at www.latestbit.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package org.latestbit.smartobjects.encrypters;

import java.nio.ByteBuffer;
import java.security.GeneralSecurityException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
//import javax.crypto.SecretKeyFactory;
//import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.SecretKeySpec;

import org.latestbit.smartobjects.io.ResizableByteBuffer;

public class StdSymmetricEncrypter extends StdAbstractDataEncrypter implements ISymmetricDataEncrypter {			

	public static final String DEFAULT_ALGORITHM 		 = "AES/ECB/PKCS5Padding";
	public static final int KEY_LENGTH = 128;
	
	public StdSymmetricEncrypter() {
		super(DEFAULT_ALGORITHM);
	}
	
	public StdSymmetricEncrypter(String algorithm) {
		super(algorithm);
	}
	
	@Override
	public void initCipher( SecretKey key ) throws InvalidKeyException, InvalidKeySpecException {
		try {
			//this.algorithm = key.getAlgorithm();
			encCipher = Cipher.getInstance(algorithm);
			decCipher = Cipher.getInstance(algorithm);
			// Key key = ((Key)this.properties.get(SECRET_PROPERTY_KEY));
			// int maxAllowedKeyLength = Cipher.getMaxAllowedKeyLength(algo);
			// Key key = new SecretKeySpec(keyText.getBytes("US-ASCII"), algo);
			encCipher.init(Cipher.ENCRYPT_MODE, key);
			decCipher.init(Cipher.DECRYPT_MODE, key);
		} 
		catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} 
		catch (NoSuchPaddingException e) {
			e.printStackTrace();
		}
	}

	@Override
	public SecretKey generateSecret() throws GeneralSecurityException {
		KeyGenerator keyGen = KeyGenerator.getInstance(this.algorithmWithoutOptions);
		keyGen.init(KEY_LENGTH);
	    SecretKey secretKey = keyGen.generateKey();
	    return secretKey;
	}

	@Override
	public SecretKey decodeSecretKey(ByteBuffer buffer) throws InvalidKeySpecException, NoSuchAlgorithmException {
		byte[] encodedKey = new byte[buffer.limit() - buffer.position()];
		buffer.get(encodedKey);		
		SecretKeySpec spec = new SecretKeySpec( encodedKey, this.algorithmWithoutOptions );
		return spec;
		// return SecretKeyFactory.getInstance( this.algorithm ).generateSecret( spec );
	}

	@Override
	public void encodeSecretKey(SecretKey key, ResizableByteBuffer buffer) {
		buffer.put(key.getEncoded());
	}
	
}
